<?php
require_once '../application/library/controller/gcLBasicAction.php';
require_once '../application/library/exception/gcExceptionDealer.php';
require_once '../application/modules/goods/constant/goods_constant_templatName.php';
require_once '../application/modules/goods/constant/goods_constant_constant.php';
require_once '../application/modules/common/services/common_services_categoryTagService.php';
require_once '../application/modules/goods/services/goods_services_goodsFormulaService.php';


/**
 * 商品一览Controller
 * 商品管理
 * @author    Suda
 * @version   1.0
 */
class goods_goodsFormulaController extends gcLBasicAction {

    private $objService;
    private $objCategoryTagService;
    /**
     * 初期表示
     */
    public function indexAction() {
        try {
            $this->pvSetLabel();
            $this->objService = new goods_services_goodsFormulaService();
            $intCount = $this->objService->pbGetCount();
            $objPager=$this->pagerInit($intCount, 1, 10, pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
            $arrGoods = $this->objService->pbGetGoods(null);
            $arrMsgId = array("ERR-100002");
            $this->setMessageForJs($arrMsgId);
            $this->objCategoryTagService       = new common_services_categoryTagService();
            $this->gcView->arrGoodsFirstCATG   = $this->objCategoryTagService->pbGetGoodsFirstCategory();
            if ($arrCondition["FirstCatg"] != "" && $arrCondition["FirstCatg"] != null) {
                $this->gcView->arrGoodsSecondCATG = $this->objCategoryTagService->pbGetGoodsSecondCategory($arrCondition["FirstCatg"]);
            } else {}
            $this->gcView->selGoodsFirstCATG   = $arrCondition["FirstCatg"];
            $this->gcView->selGoodsSecondCATG  = $arrCondition["SecondCatg"];
            $this->gcView->intGoodsNum = $intCount;
            $this->gcView->arrGoods = $arrGoods;
            $this->gcView->arrCondition = $arrCondition;
            $this->gcView->strNumFlg    = $arrCondition["StockNumFg"];
            $this->gcView->strAddEditFg = "1";
            $this->gcView->render(goods_constant_templatName::GOODS_FORMULA);
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    public function formulaeditAction() {
        try {
            $this->pvSetLabel();
            $strFormulaNo = $this->getParam("strFormulaNo");
            $this->objService = new goods_services_goodsFormulaService();
            $arrFormulaItemInfo = $this->objService->pbGetFormulaItemInfo($strFormulaNo);
            $arrFormulaInfo = $this->objService->pbGetFormulaInfo($strFormulaNo);
            $intCount = $this->objService->pbGetCount();
            $objPager=$this->pagerInit($intCount, 1, 10, pageSizeGroup::PAGE_SIZE_TEN, display::SHOW);
            $arrGoods = $this->objService->pbGetGoods($arrFormulaItemInfo);
            $arrMsgId = array("ERR-100002");
            $this->setMessageForJs($arrMsgId);
            $this->objCategoryTagService       = new common_services_categoryTagService();
            $this->gcView->arrGoodsFirstCATG   = $this->objCategoryTagService->pbGetGoodsFirstCategory();
            if ($arrCondition["FirstCatg"] != "" && $arrCondition["FirstCatg"] != null) {
                $this->gcView->arrGoodsSecondCATG = $this->objCategoryTagService->pbGetGoodsSecondCategory($arrCondition["FirstCatg"]);
            } else {}
            $this->gcView->selGoodsFirstCATG   = $arrCondition["FirstCatg"];
            $this->gcView->selGoodsSecondCATG  = $arrCondition["SecondCatg"];
            $this->gcView->intGoodsNum = $intCount;
            $this->gcView->arrGoods = $arrGoods;
            $this->gcView->arrCondition = $arrCondition;
            $this->gcView->strNumFlg    = $arrCondition["StockNumFg"];
            $this->gcView->strAddEditFg = "2";
            $this->gcView->FormulaNo = $strFormulaNo;
            $this->gcView->GoodsNo  = $arrFormulaInfo["GOODS_NO"];
            $this->gcView->arrFormulaItemInfo = $arrFormulaItemInfo;
            $this->gcView->arrFormulaInfo = $arrFormulaInfo;
            $this->gcView->render(goods_constant_templatName::GOODS_FORMULA);
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }
    /**
     * 初期化页面情报
     * pvSetLabel
     * @param null
     * @return null
     */
    private function pvSetLabel() {
        $this->gcView->title                          = $this->getLabel("goods_EC0005_PageTitle");
        $this->gcView->goods_EC0005_H1                = $this->getLabel("goods_EC0005_H1");
        $this->gcView->goods_EC0005_H2                = $this->getLabel("goods_EC0005_H2");
        $this->gcView->goods_EC0005_FormulaName       = $this->getLabel("goods_EC0005_FormulaName");
        $this->gcView->goods_EC0005_GoodsCode         = $this->getLabel("goods_EC0005_GoodsCode");
        $this->gcView->goods_EC0005_GoodsName         = $this->getLabel("goods_EC0005_GoodsName");
        $this->gcView->goods_EC0005_SalesPrice        = $this->getLabel("goods_EC0005_SalesPrice");
        $this->gcView->goods_EC0005_Yen               = $this->getLabel("goods_EC0005_Yen");
        $this->gcView->goods_EC0005_addGoods          = $this->getLabel("goods_EC0005_addGoods");
        $this->gcView->goods_EC0005_Link              = $this->getLabel("goods_EC0005_Link");
        $this->gcView->goods_EC0005_Class             = $this->getLabel("goods_EC0005_Class");
        $this->gcView->goods_EC0005_InNum             = $this->getLabel("goods_EC0005_InNum");
        $this->gcView->goods_EC0005_GetAll            = $this->getLabel("goods_EC0005_GetAll");
        $this->gcView->goods_EC0005_GetLeft           = $this->getLabel("goods_EC0005_GetLeft");
        $this->gcView->goods_EC0005_GetExcut          = $this->getLabel("goods_EC0005_GetExcut");
        $this->gcView->goods_EC0005_GetSet            = $this->getLabel("goods_EC0005_GetSet");
        $this->gcView->goods_EC0005_SearCondition     = $this->getLabel("goods_EC0005_SearCondition");
        $this->gcView->goods_EC0005_Clear             = $this->getLabel("goods_EC0005_Clear");
        $this->gcView->goods_EC0005_SalePrice         = $this->getLabel("goods_EC0005_SalePrice");
        $this->gcView->goods_EC0005_Title2            = $this->getLabel("goods_EC0005_Title2");
        $this->gcView->goods_EC0005_Label             = $this->getLabel("goods_EC0005_Label");
        $this->gcView->goods_EC0005_Num               = $this->getLabel("goods_EC0005_Num");
        $this->gcView->goods_EC0005_Product           = $this->getLabel("goods_EC0005_Product");
        $this->gcView->goods_EC0005_Operation         = $this->getLabel("goods_EC0005_Operation");
        $this->gcView->goods_EC0005_Delete            = $this->getLabel("goods_EC0005_Delete");
        $this->gcView->goods_EC0005_Set               = $this->getLabel("goods_EC0005_Set");
        $this->gcView->goods_EC0005_Back              = $this->getLabel("goods_EC0005_Back");
    }

    /**
     * 检索
     */
    public function searchAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                if ($this->getParam("Flag") == "1") {
                    $arrCondition = array("GoodsCode"  => $this->getParam("GoodsCode"),
                                      "GoodsName"  => $this->getParam("GoodsName"),
                                      "PriceFrom"  => $this->getParam("PriceFrom"),
                                      "PriceTo"    => $this->getParam("PriceTo"),
                                      "FirstCatg"    => $this->getParam("FirstCatg"),
                                      "SecondCatg"    => $this->getParam("SecondCatg"),
                                      "StockNumFg"    => $this->getParam("StockNumFg"),
                                      "StockNumFrom"    => $this->getParam("StockNumFrom"),
                                      "StockNumTo"    => $this->getParam("StockNumTo"));
                    sessionHelper::setPageData(goods_constant_constant::GOODS_LIST, $arrCondition);
                } else {
                    $arrCondition = sessionHelper::getPageData(goods_constant_constant::GOODS_LIST);
                    $arrCondition["pageSize"] = $this->getParam("pageSize");
                    $arrCondition["orderColumn"] = $this->getParam("orderColumn");
                    $arrCondition["order"] = $this->getParam("order");
                }
                $arrGoodsId = $this->getParam("FormulaGoodsNo",true);
                $this->objService   = new goods_services_goodsFormulaService();
                $intCount = $this->objService->pbGetSearchCount($arrCondition);
                $objPager = $this->getPagerParam($intCount);
                if ($intCount != 0) {
                    $arrCondition["offset"]      = $objPager->getOffset();
                    $arrCondition["limit"]        = $objPager->getLimit();
                    $CurPage                         = $objPager->getCurPage();

                    $arrData = $this->objService->pbSearchGoods($arrCondition,$arrGoodsId);
//                    var_dump($arrData);
//                    var_dump($intCount);var_dump($arrData);exit;
                    $arrResault = array(
                        "count"      => $intCount,
                        "curPage"      => $CurPage,
                        "searchData" => $arrData
                    );
//                    var_dump($arrResault);exit;
                } else {
                    $arrResault = array(
                        "count"      => 0,
                        "curPage"    => $objPager->getCurPage(),
                        "searchData" => "");
//                    var_dump($arrData);exit;
                }
                jsonHelper::outPutJson($arrResault);
            } else {
                gcExceptionDealer::throwGcException();
            }
        } catch(Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }

    public function saveAction() {
        try {
            if ($this->getRequest()->isXmlHttpRequest()) {
                $strAddEditFg           = $this->getParam("AddEditFg");
                $arrFormulaGoodsName    = $this->getParam("FormulaGoodsName",true);
                $arrFormulaGoodsNo      = $this->getParam("FormulaGoodsNo",true);
                $arrFormulaGoodsNum     = $this->getParam("FormulaGoodsNum",true);
                $strFormulaName         = $this->getParam("FormulaName");
                $arrProductInfo = array("ProductName" => $this->getParam("ProductName"),
                                        "ProductCode" => $this->getParam("ProductCode"),
                                        "ProductTp"   => $this->getParam("GoodsTp"));
                foreach($arrFormulaGoodsNo as $key => $value) {
                    $arrFormulaGoodsInfo[$key]["GoodsNo"] = $value;
                    $arrFormulaGoodsInfo[$key]["GoodsName"] = $arrFormulaGoodsName[$key ];
                    $arrFormulaGoodsInfo[$key]["Num"] = $arrFormulaGoodsNum[$key ];
                }
                $this->objService = new goods_services_goodsFormulaService();
                if($strAddEditFg == "1") {
                    $strGoodsNo = $this->objService->pbInsertGoods($arrProductInfo);
                    $strFormulaNo = $this->objService->pbInsertFormula($strGoodsNo, $strFormulaName);
                    $this->objService->pbInsertFormulaItem($arrFormulaGoodsInfo, $strFormulaNo);
                } else {
                    $strFormulaNo = $this->getParam("FormulaNo");
                    $strGoodsNo = $this->getParam("GoodsNo");
                    $this->objService->pbUpdateGoods($strGoodsNo, $arrProductInfo);
                    $this->objService->pbUpdateFomula($strFormulaNo, $strFormulaName);
                    $this->objService->pbUpdateFormulaItem($strFormulaNo, $arrFormulaGoodsInfo);
                }
                $output = array();
                jsonHelper::outPutJson($output);
            } else {
                gcExceptionDealer::throwAjaxException();
            }
        } catch (Exception $e) {
            gcExceptionDealer::setException($e);
        }
    }
}
